package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.domain.WdyBuyProduct;
import com.ruoyi.system.domain.WdyProduct;
import com.ruoyi.system.mapper.WdyProductMapper;
import com.ruoyi.system.service.IWdyBuyProductService;
import com.ruoyi.system.service.IWdyProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 理财产品 服务层实现
 * 
 * @author ruoyi
 * @date 2019-12-15
 */
@Service
public class WdyProductServiceImpl extends ServiceImpl<WdyProductMapper, WdyProduct> implements IWdyProductService
{
	@Autowired
	private IWdyBuyProductService wdyBuyProductService;
	/**
     * 查询理财产品信息
     * 
     * @param id 理财产品ID
     * @return 理财产品信息
     */
    @Override
	public WdyProduct selectWdyProductById(Long id)
	{
	    return baseMapper.selectWdyProductById(id);
	}
	
	/**
     * 查询理财产品列表
     * 
     * @param wdyProduct 理财产品信息
     * @return 理财产品集合
     */
	@Override
	public List<WdyProduct> selectWdyProductList(WdyProduct wdyProduct)
	{
	    return baseMapper.selectWdyProductList(wdyProduct);
	}
	
    /**
     * 新增理财产品
     * 
     * @param wdyProduct 理财产品信息
     * @return 结果
     */
	@Override
	public int insertWdyProduct(WdyProduct wdyProduct)
	{
	    return baseMapper.insertWdyProduct(wdyProduct);
	}
	
	/**
     * 修改理财产品
     * 
     * @param wdyProduct 理财产品信息
     * @return 结果
     */
	@Override
	public int updateWdyProduct(WdyProduct wdyProduct)
	{
	    return baseMapper.updateWdyProduct(wdyProduct);
	}

	/**
     * 删除理财产品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWdyProductByIds(String ids)
	{
		return baseMapper.deleteWdyProductByIds(Convert.toStrArray(ids));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void buyProduct(String userId, long id) {
		WdyProduct product = this.getById(id);
		if(product == null) {
			throw new BusinessException(ResultConstants.FI1000, "product " + MessageUtils.message("not.exists"));
		}
		WdyBuyProduct buyProduct = new WdyBuyProduct();
		buyProduct.setUserId(userId);
		buyProduct.setVcAmount(product.getVcAmount());
		buyProduct.setCnnAmount(product.getCnnAmount());
		buyProduct.setReleaseAmount(product.getReleaseVcAmount());
		buyProduct.setFreezeVcAmount(product.getVcAmount());
		wdyBuyProductService.buy(buyProduct);
	}
}
